﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using ECCSystem.BizRule;

namespace ECCSystem.DataAccess
{
	class ClsRule : DBObject 
    {
		public DataSet GetRule()
		{
			return RunProcedure("sp_Rule_GetList", new IDataParameter[] { }, "Rule");
		}

		public DataSet GetEmailTempletList()
		{
			SqlParameter[] parameters = new SqlParameter[] {
													new SqlParameter("@UserID", SqlDbType.Int)
															};
			parameters[0].Value = ClsCommon.gIntOperatorID;
			return RunProcedure("sp_EmailTemplet_GetList", parameters, "EmailTemplet");
		}

		public DataSet GetRule(string strWhere)
		{
			SqlParameter[] parameters = new SqlParameter[] {
													new SqlParameter("@where", SqlDbType.NVarChar,1000)
															};
			parameters[0].Value = strWhere;
			return RunProcedure("sp_Rule_GetListByWhere", parameters, "Rule");
		}
		
		public string AddRule(string strCaption, string strContent, int intLimited,
							DateTime dtStartTime, DateTime dtEndTime, string strUserName,
							string strUserID, int intUserID, string[] strKeyID,
							SqlTransaction objTran)
        {
			try
			{
				int rowsAffected = 0;
				SqlCommand objCmd = new SqlCommand("sp_Rule_Add", BizRule.ClsCommon.gObjConn);
				SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Caption", SqlDbType.NVarChar,100),
															 new SqlParameter("@Content", SqlDbType.NText),
															 new SqlParameter("@Limited", SqlDbType.Bit),
															 new SqlParameter("@StartDate", SqlDbType.DateTime),
															 new SqlParameter("@EndDate", SqlDbType.DateTime),
															 new SqlParameter("@Member", SqlDbType.NVarChar,1000),
															 new SqlParameter("@MemberID", SqlDbType.VarChar,1000),
															 new SqlParameter("@CreatePerson", SqlDbType.Int),
															 new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier)
															};

				parameters[0].Value = strCaption;
				parameters[1].Value = strContent;
				parameters[2].Value = intLimited;
				if (intLimited == 1)
				{
					parameters[3].Value = dtStartTime;
					parameters[4].Value = dtEndTime;
				}
				else
				{
					parameters[3].Value = System.DBNull.Value;
					parameters[4].Value = System.DBNull.Value;
				}

				parameters[5].Value = strUserName;
				parameters[6].Value = strUserID;
				parameters[7].Value = intUserID;
				string strguid = Guid.NewGuid().ToString();
				Guid guid = new Guid(strguid);
				parameters[8].Value = guid;

				this.RunProcedure("sp_Rule_Add", parameters, ref rowsAffected, objTran, objCmd);

				if (rowsAffected == 0)
				{
					objTran.Rollback();
					return "1";
				}
				objCmd.CommandText = "sp_EmailTempletAndRule_Update";

				for (int i = 0; i < strKeyID.Length; i++)
				{
					parameters = null;

					parameters = new SqlParameter[] { new SqlParameter("@ContentKeyID", SqlDbType.UniqueIdentifier),
															 new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier),
															 new SqlParameter("@UserID", SqlDbType.Int),
															 new SqlParameter("@RuleUser",SqlDbType.VarChar,1000)
															};

					guid = new Guid(strKeyID[i]);
					parameters[0].Value = guid;
					guid = new Guid(strguid);
					parameters[1].Value = guid;
					parameters[2].Value = intUserID;
					parameters[3].Value = strUserID;
					this.RunProcedure("sp_EmailTempletAndRule_Update", parameters, ref rowsAffected, objTran, objCmd);

					if (rowsAffected == 0)
					{
						objTran.Rollback();
						return "1";
					}
				}

				objTran.Commit();
				return "";
			}
			catch (Exception ex)
			{
				objTran.Rollback();
				return ex.ToString();
			}

        }

		public void DeleteRule(string strID)
		{
			SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@ID", SqlDbType.UniqueIdentifier) };
			Guid guid = new Guid(strID);
			parameters[0].Value = guid;
			this.RunProcedure("sp_Rule_Delete", parameters, "Rule");
		}

		public string UpdateRule(string strID, string strCaption, string strContent, int intLimited,int intUserID,
							DateTime dtStartTime, DateTime dtEndTime, string strUserName, string strUserID, string[] strKeyID,string[] strFlg,
							SqlTransaction objTran)
		{



			try
			{
				int rowsAffected = 0;
				SqlCommand objCmd = new SqlCommand("sp_Rule_Update", BizRule.ClsCommon.gObjConn);
				SqlParameter[] parameters = new SqlParameter[] { new SqlParameter("@Caption", SqlDbType.NVarChar,100),
															 new SqlParameter("@Content", SqlDbType.NText),
															 new SqlParameter("@Limited", SqlDbType.Bit),
															 new SqlParameter("@StartDate", SqlDbType.DateTime),
															 new SqlParameter("@EndDate", SqlDbType.DateTime),
															 new SqlParameter("@Member", SqlDbType.NVarChar,1000),
															 new SqlParameter("@MemberID", SqlDbType.VarChar,1000),
															 new SqlParameter("@ID", SqlDbType.UniqueIdentifier)};

				parameters[0].Value = strCaption;
				parameters[1].Value = strContent;
				parameters[2].Value = intLimited;
				if (intLimited == 1)
				{
					parameters[3].Value = dtStartTime;
					parameters[4].Value = dtEndTime;
				}
				else
				{
					parameters[3].Value = System.DBNull.Value;
					parameters[4].Value = System.DBNull.Value;
				}
				parameters[5].Value = strUserName;
				parameters[6].Value = strUserID;
				Guid guid = new Guid(strID);
				parameters[7].Value = guid;
				this.RunProcedure("sp_Rule_Update", parameters, ref rowsAffected, objTran, objCmd);

				if (rowsAffected == 0)
				{
					objTran.Rollback();
					return "1";
				}
				objCmd.CommandText = "sp_EmailTempletAndRule_Update";

				for (int i = 0; i < strKeyID.Length; i++)
				{
					if (strKeyID[i] == null)
					{
						break;
					}
					parameters = null;

					parameters = new SqlParameter[] { new SqlParameter("@ContentKeyID", SqlDbType.UniqueIdentifier),
															 new SqlParameter("@KeyID", SqlDbType.UniqueIdentifier),
															 new SqlParameter("@UserID", SqlDbType.Int),
															new SqlParameter("@RuleUser",SqlDbType.VarChar,1000)
															};

					guid = new Guid(strKeyID[i]);
					parameters[0].Value = guid;
					if (strFlg[i] == null)
					{
						parameters[1].Value = System.DBNull.Value;
						parameters[3].Value = System.DBNull.Value;
					}
					else
					{
						guid = new Guid(strID);
						parameters[1].Value = guid;
						parameters[3].Value = strUserID;
					}
					
					
					parameters[2].Value = intUserID;
					
					this.RunProcedure("sp_EmailTempletAndRule_Update", parameters, ref rowsAffected, objTran, objCmd);

					if (rowsAffected == 0)
					{
						objTran.Rollback();
						return "1";
					}
				}

				objTran.Commit();
				return "";
			}
			catch (Exception ex)
			{
				objTran.Rollback();
				return ex.ToString();
			}

		}
    }
}
